package com.yunxi.service.esb.impl;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.yunxi.core.page.PageHelperPlus;
import com.yunxi.core.util.BeanTools;
import com.yunxi.core.util.HttpUtil;
import com.yunxi.core.util.StringUtil;
import com.yunxi.core.web.ApiResult;
import com.yunxi.core.web.ResponseUtil;
import com.yunxi.eai.pojo.esb.EaiManageLogVO;
import com.yunxi.eai.service.esb.ESBManageService;
import com.yunxi.model.eai.EaiLog;
import com.yunxi.service.eai.EaiLogService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2020/1/7.
 */
public class ESBManageServiceImpl implements ESBManageService {
    protected static final Logger _log = LoggerFactory.getLogger(ESBManageServiceImpl.class);

    @Autowired
    @Qualifier("eaiLogServiceImpl")
    EaiLogService eaiLogService;

    @Override
    public PageInfo findPageByVO(EaiManageLogVO eaiManageLogVO) {

        //  PageInfo<EaiManageLogVO> pageInfo = eaiLogService.pageByVo(eaiManageLogVO, pageNum, pageSize);
        Map map = BeanTools.bean2map(eaiManageLogVO);
        PageHelperPlus.startPage(eaiManageLogVO.getPageNum(), eaiManageLogVO.getPageSize());
        List<Object> list = eaiLogService.queryByMap(map);
        PageInfo<Object> pageInfo = new PageInfo<Object>(list);
        return pageInfo;
    }

    @Override
    public List listByVo(EaiManageLogVO eaiManageLogVO) {
        return null;
    }

    @Override
    public EaiManageLogVO getById(String logId) {
        return null;
    }

    @Override
    public ApiResult resend(EaiManageLogVO eaiManageLogVO) {
        ApiResult apiResult = ResponseUtil.ok();
        if (StringUtils.isEmpty(eaiManageLogVO.getUrl())) {
            apiResult = ResponseUtil.fail(400, "EAI发送给ESB的URL为空，请通过Postman触发!");
            return apiResult;
        }
        if (StringUtils.isEmpty(eaiManageLogVO.getServiceName())) {
            apiResult = ResponseUtil.fail(400, "EAI发送给ESB的ServiceName为空，请通过Postman触发!");
            return apiResult;
        }
        if (StringUtils.isEmpty(eaiManageLogVO.getSourceSystem())) {
            apiResult = ResponseUtil.fail(400, "EAI发送给ESB的SourceSystem为空，请通过Postman触发!");
            return apiResult;
        }
        if (StringUtils.isEmpty(eaiManageLogVO.getPmsEaiContent())) {
            apiResult = ResponseUtil.fail(400, "EAI发送给ESB的PmsEaiContent为空，请通过Postman触发!");
            return apiResult;
        }

        if (eaiManageLogVO.getType() == 0) {
            //同步失败接口，异步由定时任务触发
            EaiLog eaiLog = new EaiLog();
            try {
                Map map = HttpUtil.sendPost(eaiManageLogVO.getPmsEaiContent(), eaiManageLogVO.getServiceName(), eaiManageLogVO.getSourceSystem(), eaiManageLogVO.getUrl());
                eaiLog.setRequestSendTime(new Date());
                eaiLog.setRequestId(String.valueOf(map.get("requestId")));
                eaiLog.setResponseReceiveTime(new Date());
                eaiLog.setEsbResultContent(JSONObject.toJSONString(map));
                Object object = map.get("header");
                Map<String, Object> headerMap = BeanTools.objectToMap(object);
                String statusFlag = String.valueOf(headerMap.get("statusFlag"));
                String response = String.valueOf(map.get("response"));
                if (statusFlag.equals("0")) {//这里只对ESB的处理状态做判断
                    //处理失败，更状态位失败
                    eaiLog.setDisposeStatus(2);
                    apiResult = ResponseUtil.fail(400, "ESB操作失败!");
                    return apiResult;
                } else {
                    JSONObject jsonResponse = JSONObject.parseObject(response);
                    if (!String.valueOf(jsonResponse.get("code")).equals("S") || !String.valueOf(jsonResponse.get("code")).equals("200")) {
                        //处理失败，更状态位失败
                        if (eaiManageLogVO.getDisposeStatus() == 3) {
                            eaiLog.setDisposeStatus(3);
                        } else {
                            eaiLog.setDisposeStatus(2);
                        }
                        eaiLog.setDisposeCount(eaiLog.getDisposeCount() + 1);
                    } else {
                        eaiLog.setDisposeStatus(1);
                    }
                }
            } catch (Exception e) {
                _log.error(e.getMessage(), e);
                eaiLog.setDisposeCount(eaiLog.getDisposeCount() + 1);
                eaiLog.setDisposeStatus(2);
            }
            eaiLog.setUpdateDate(new Date());
            eaiLogService.saveOrUpdate(eaiLog);
            if (eaiLog.getDisposeStatus() == 1) {
                EaiLog oldEaiLog = new EaiLog();
                BeanUtils.copyProperties(eaiManageLogVO, oldEaiLog);
                oldEaiLog.setDisposeStatus(5);
                eaiLogService.saveOrUpdate(oldEaiLog);
            }
        } else {
            apiResult = ResponseUtil.fail(400, "异步由定时任务触发!");
            return apiResult;
        }
        return apiResult;
    }
}
